package com.gc.controller;

import com.gc.common.resp.ApiResult;
import com.gc.dto.EntrustDTO;
import com.gc.pojo.SysClient;
import com.gc.pojo.SysEntrust;
import com.gc.pojo.SysUser;
import com.gc.services.ClientService;
import com.gc.services.EntrustService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("entrust")
public class EntrustController {

    @Autowired
    private EntrustService entrustService;
    @Autowired
    private ClientService clientService;

    @PostMapping("submit")
    @ResponseBody
    public ApiResult submit(@RequestBody SysEntrust entrust){
        Subject subject = SecurityUtils.getSubject();
        SysUser currentUser = (SysUser) subject.getPrincipal();
        if(currentUser == null){
            return new ApiResult(500,"未登录!");
        }
        entrust.setUserId(currentUser.getUserId());
        entrust.setCreateTime(new Date());
        int count = entrustService.addEntrust(entrust);
        if(count != 1){
            return new ApiResult(500 ,"插入失败!");
        }
        return new ApiResult(200,"提交成功!");
    }

    @GetMapping("submit-view")
    public ModelAndView toSubmitView() {
        ModelAndView mav = new ModelAndView("entrust/submit");
        //查询所有公司 用于填写委托
        List<SysClient> clients = clientService.getAllClient();
        mav.addObject("clients",clients);
        return mav;
    }
    @GetMapping("record-view")
    public ModelAndView toRecordView() {
        ModelAndView mav = new ModelAndView("entrust/record");
        return mav;
    }

//    @RequiresPermissions("entrust:getMyEntrust")
    @GetMapping("all")
    @ResponseBody
    public List<EntrustDTO> all() {
        List<EntrustDTO> clients = entrustService.getAllDTO();
        return clients;
    }
}
